<?php

/*
消息发送action
*/

include '../include/init.php';


//var_dump($_POST);
//var_dump($_FILES['partpic']);
//exit;



$mode=$_POST['md'];



if($mode=="text")
{
	$comet=new cometModel();
	$receivertype=$_POST['rt'];
	$data['fromid']=$_POST['u'];
	$data['type']=$_POST['dt'];

	$data['toid']=$_POST['to'];
	$data['mode']=$mode;
	$data['time']=time();

	$data['content']=$_POST['cnt'];
	if($data['type']!='file'&&$data['type']!='pic')
	{
		if($receivertype=='frd')
		{
			$cmid=$comet->add($data);
			if($cmid)
			{
				echo 'ok:time='.Date('Y/m/d,H:i:s',$data['time']).';'.$cmid;
			}
			else
			{
				echo 'no';
			}
		}
		else if($receivertype=='grp')
		{
		}
		else if($receivertype=='dis')
		{

		}
		exit;
	}
}
else if($mode=="shake")
{
	$comet=new cometModel();
	$receivertype=$_POST['rt'];
	$data['fromid']=$_POST['u'];
	$data['type']=$_POST['dt'];

	$data['toid']=$_POST['to'];
	$data['mode']=$mode;
	$data['time']=time();

	$data['content']=$_POST['cnt'];
	$cmid=$comet->add($data);
	if($cmid)
	{
		echo 'ok:time='.Date('Y/m/d,H:i:s',$data['time']).';'.$cmid;
	}
	else
	{
		echo 'no';
	}
	exit;
}
else if($mode=="pic")
{
	if(!isset($_POST['cnt']))
	{
		$filename=$_POST['picname'];
		if(!is_dir(ROOT.'upload'))
		{
			mkdir(ROOT.'upload');
		}
		if(!file_exists(ROOT.'upload/'.$filename))
		{
			move_uploaded_file($_FILES['partpic']['tmp_name'],ROOT.'upload/'.$filename);
		}
		else {
			file_put_contents(ROOT.'upload/'.$filename,file_get_contents($_FILES['partpic']['tmp_name']),FILE_APPEND);
		}
	}
	else
	{
		$comet=new cometModel();
		$receivertype=$_POST['rt'];
		$data['fromid']=$_POST['u'];
		$data['type']=$_POST['dt'];

		$data['toid']=$_POST['to'];
		$data['mode']=$mode;
		$data['time']=time();


		$filename=$_POST['cnt'];

		$data['content']="[img][upload/".$filename."][/img]";
		$cmid=$comet->add($data);
		if($cmid)
		{
			echo 'ok:time='.Date('Y/m/d,H:i:s',$data['time']).';'.$cmid;
		}
		else
		{
			echo 'no';
		}
		exit;
	}
}
else if($mode=="spic")
{
	$filename=$_POST['fn'];
	move_uploaded_file($_FILES['img']['tmp_name'],ROOT.'upload/'.$filename);
	echo 'ok';
}